﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace UDS.Web.UI.Controls
{
    /// <summary>
    /// Add the jQuery support for web pages.
    /// </summary>
    public class CommonJScript : WebControl
    {
        [PersistenceMode(PersistenceMode.Attribute)]
        [DefaultValue(true)]
        public bool EnableJQuery
        {
            get;
            set;
        }

        [PersistenceMode(PersistenceMode.Attribute)]
        [DefaultValue(true)]
        public bool EnableJQueryUI
        {
            get;
            set;
        }

        public CommonJScript()
        {
            EnableJQuery = true;
            EnableJQueryUI = true;
        }

        /// <summary>
        /// Renders the control.
        /// </summary>
        protected override void Render(HtmlTextWriter output)
        {
            if (EnableJQuery)
            {
#if DEBUG
                RenderScript(output, ResolveUrl("~/common/jquery-1.3.2.js"));
#else
                RenderScript(output, ResolveUrl("~/common/jquery-1.3.2.min.js"));
#endif
                if (EnableJQueryUI)
                {
                    RenderScript(output, ResolveUrl("~/common/jquery-ui-1.7.2.custom.min.js"));
                }
            }
        }

        private static void RenderScript(HtmlTextWriter output, string src)
        {
            output.WriteBeginTag("script");
            output.WriteAttribute("type", "text/javascript");
            output.WriteAttribute("src", src);
            output.Write('>');
            output.WriteEndTag("script");
        }
    }
}